package com.newdriver.edu.inf.manager.controller;

import com.newdriver.edu.inf.manager.domain.Teacher;
import com.newdriver.edu.inf.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    public void start() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choise = sc.next();
            switch (choise) {
                case "1":
                    //System.out.println("添加");
                    addTracher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeaById();
                    break;
                case "3":
                    // System.out.println("修改");
                    undateTeacher();
                    break;
                case "4":
                    //System.out.println("查看");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用");
                    break lo;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void findAllTeacher() {
        Teacher[] teas=teacherService.findAllTeacher();
        //判断是否为null
        if (teas==null) {
            System.out.println("暂无信息 先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t\t生日");
        //遍历打印
        for (int i = 0; i < teas.length; i++) {
            Teacher tea = teas[i];
            if (tea!=null) {
                System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t\t"+tea.getBir());
            }
        }

    }


    public void undateTeacher() {
        String updateId;
        if (teacherService.findAllTeacher()==null) {
            System.out.println("空");
            return;
        }

        while (true) {
            System.out.println("输入要修改的教师id");
            updateId = sc.next();
            boolean exists = teacherService.isExists(updateId);
            if(!exists){
                System.out.println("要修改的id不存在");
            }else{
                break;
            }
        }
        System.out.println("输入教师姓名");
        String name = sc.next();
        System.out.println("输入教师年龄");
        String age = sc.next();
        System.out.println("输入教师生日");
        String bir = sc.next();

        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBir(bir);

        teacherService.updateTeacher(updateId,teacher);
        System.out.println("修改成功");
    }

    public void deleteTeaById() {
        String delId;
       if(teacherService.findAllTeacher()==null){
           System.out.println("空");
           return;
       }

        while (true) {
            System.out.println("输入您要删除的教师id");
            delId = sc.next();
            boolean exists = teacherService.isExists(delId);
            if (!exists) {
                System.out.println("您要删除的id不存在");
            }else{
                break;
            }
        }
        teacherService.deleteTeaById(delId);
        System.out.println("删除成功");
    }

    public void addTracher() {
        String id;

        while (true) {
            System.out.println("输入id");
            id = sc.next();
            boolean flag=teacherService.isExists(id);
            if(flag){
                System.out.println("工号已被占用");
            }else{
                break;
            }
        }

        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        String age = sc.next();
        System.out.println("输入生日");
        String bir = sc.next();

        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBir(bir);

        boolean result = teacherService.addTeacher(teacher);
        if(result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }


}
